package com.mlnx.web.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.analysis.pojo.dto.dynamic.DynamicEcgAnalysisResult;
import com.mlnx.common.entity.Response;
import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.device.service.DeviceStateService;
import com.mlnx.ui.entity.EcgReport;
import com.mlnx.ui.service.EcgReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(value = "UI心电接口", tags = "UI心电接口")
@RestController
@RequestMapping("ui/ecg")
public class EcgUiController extends BaseController {
    @Autowired
    private EcgReportService ecgReportService;

    @Autowired
    private DeviceStateService deviceStateService;

    @ApiOperation(value = "获取某月中有心电数据存在的天")
    @GetMapping(value = "/day")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", required = true),
            @ApiImplicitParam(name = "month", value = "月", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getEcgDays(@RequestParam("year") Integer year, @RequestParam("month") Integer month,
                               @RequestParam("patientId") Integer patientId) {
        Set<Date> ecgExistedDays = deviceStateService.getEcgExistedDays(year, month, patientId);

        return result(ecgExistedDays);
    }

    @ApiOperation(value = "获取设备上下线时间标记")
    @GetMapping(value = "/mark")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getEcgMarks(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime,
                                @RequestParam("patientId") Integer patientId) {
        Map<String, Object> ecgMarks = deviceStateService.getEcgMarks(new Date(startTime), new Date(endTime), patientId);

        return result(ecgMarks);
    }

    @ApiOperation(value = "保存异常标记事件")
    @PostMapping(value = "/event")
    public Response saveEcgEvent(@RequestBody DeviceStateRecord deviceStateRecord) {
        deviceStateService.saveDeviceState(deviceStateRecord);

        return result();
    }

    @ApiOperation(value = "获取患者紧急呼叫、异常标记事件")
    @GetMapping(value = "/event")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getEcgEvents(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime,
                                 @RequestParam("patientId") Integer patientId) {
        List<DeviceStateRecord> ecgEvents = deviceStateService.getEcgEvents(new Date(startTime), new Date(endTime), patientId);

        return result(ecgEvents);
    }

    @ApiOperation(value = "保存心电报告结果")
    @PostMapping(value = "/report")
    public Response saveEcgReport(@RequestBody DynamicEcgAnalysisResult dynamicEcgAnalysisResult) {
        ecgReportService.saveEcgReport(dynamicEcgAnalysisResult);

        return result();
    }

    @ApiOperation(value = "获取动态心电报告列表")
    @GetMapping(value = "/report")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getEcgReportList(@RequestParam("patientId") Integer patientId) {
        List<EcgReport> ecgReports = ecgReportService.list(new QueryWrapper<EcgReport>().eq("patient_id", patientId));

        return result(ecgReports);
    }

    @ApiOperation(value = "通过报告id获取动态心电报告")
    @GetMapping(value = "/report/{id}")
    public Response getEcgReportById(@PathVariable("id") Integer id) {
        Map<String, Object> report = ecgReportService.getReportById(id);

        return result(report);
    }
}
